import 'package:flutter/cupertino.dart';
import 'package:get/get_navigation/src/router_report.dart';
import 'package:zrjz/comm/utils/log_utils.dart';
import 'package:zrjz/router/my_router_history_manager.dart';

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

/// 手动让getx感知原生路由 (固定代码无需修改)
class GetXRouterObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    RouterReportManager.reportCurrentRoute(route);
    MyRouterHistoryManager().putRouterByName(route.settings.name);
    Log.d('添加之后-当前的My路由表：${MyRouterHistoryManager().routeNames.toString()}');
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) async {
    RouterReportManager.reportRouteDispose(route);
    MyRouterHistoryManager().removeRouterByName(route.settings.name);
    Log.d('Pop之后-当前的My路由表：${MyRouterHistoryManager().routeNames.toString()}');
  }

  @override
  void didRemove(Route route, Route? previousRoute) {
    MyRouterHistoryManager().removeRouterByName(route.settings.name);
    Log.d('Remove之后-当前的My路由表：${MyRouterHistoryManager().routeNames.toString()}');
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    MyRouterHistoryManager().putRouterByName(newRoute?.settings.name);
    MyRouterHistoryManager().removeRouterByName(oldRoute?.settings.name);
    Log.d('Replace之后-当前的My路由表：${MyRouterHistoryManager().routeNames.toString()}');
  }
}
